﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CKFinder;
using System.Data;

public partial class Admin_EditCatProduct : System.Web.UI.Page
{
    private string id;
    CategoryController categoryController = new CategoryController();
    public List<CategoryImageProduct> cateProduct;
    protected void Page_Load(object sender, EventArgs e)
    {
        CKFinder.FileBrowser fb = new FileBrowser();
        fb.BasePath = "../ckfinder/";
        fb.SetupCKEditor(ckeditor);
        id = Request.QueryString["id"];
        cateProduct = categoryController.getCategoryImageProduct(Convert.ToInt32(id));
        if (!IsPostBack)
        {

            if (id == "" || id == null || string.IsNullOrWhiteSpace(id.ToString()) || string.IsNullOrEmpty(id.ToString()))
            {

                error();
            }
            else
            {

                int idd = Convert.ToInt32(id);
                if (!this.checkId(idd))
                {
                    error();
                }
                else
                {
                    DataTable dt = categoryController.getCategoryByID(idd);
                    Category cate = new Category(dt.Rows[0]);
                    txtName.Text = cate.Name;
                    txtOrder.Text = cate.Order + "";
                    txtTag.Text = cate.Tag;
                    txtUrl.Text = cate.Url;
                    ckeditor.Text = cate.Description;
                    if(cate.Status)
                    {
                        ddlStatus.SelectedIndex = 0;
                    }
                    else
                    {
                        ddlStatus.SelectedIndex = 1;
                    }

                }

            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Category cate = new Category();
        int idd = Convert.ToInt32(id);
        cate.Category_id = idd;
        cate.Name = txtName.Text;
        cate.Url = txtUrl.Text;
        cate.Description = ckeditor.Text;
        cate.DateUpdate = DateTime.Now;
        cate.Order = Convert.ToInt32(txtOrder.Text);
        cate.Tag = txtTag.Text;
        

        if (ddlStatus.SelectedIndex == 0)
        {
            cate.Status = true;
        }
        else
        {
            cate.Status = false;
        }
        categoryController.Update(cate);
        if (catproduct_image_value.Value != "")
        {
            string str = catproduct_image_value.Value.Substring(1);
            char[] s = { ',' };
            string[] image = str.Split(s);
            foreach (var s1 in image)
            {
                categoryController.InsertImageCat(idd,s1,1,1,DateTime.Now);
            }
        }
        Response.Write("<script>alert('Cập nhật loại sản phẩm thành công')</script>");
        Server.Transfer(Request.RawUrl);
    }
    private bool checkId(int id)
    {
        DataTable dt = categoryController.getCategoryByID(id);

        if (dt.Rows.Count > 0)
        {
            return true;
        }
        return false;
    }
    private static void error()
    {
        HttpContext.Current.Response.Redirect("~/Error.aspx");
    }
}